import traceback

from task.OperateDBHouse import OperateDBHouse
from task.MonitorLikeHouse import MonitorLikeHouse
from task.CrawlHouse import CrawlHouse
from optparse import OptionParser
import sys
import os
from baseclass.send_email import SendMail
from base_start import BaseStart


class HouseSpider(BaseStart):

    def run(self):
        parser = OptionParser()
        parser.add_option("-t", "--type", dest="type",
                          help="1-only xiaoqu 2- crawl by xiaoqu 3-house detail 4-all")
        parser.add_option("-d", "--district", dest="district",
                          help="beijing district")
        parser.add_option("-a", "--area", dest="area",
                          help="beijing area")
        parser.add_option("-x", "--xiaoqu_list", dest="xiaoqu_list",
                          help="beijing xiaoqu list")
        parser.add_option("-o", "--house", dest="house",
                          help="beijing house")
        (options, args) = parser.parse_args()
        type, district,area = options.type,options.district,options.area
        like = CrawlHouse()
        try:
            if type == "1":
                like.spider.crawl_xiaoqu_list_all_district(district,area)
            elif type == "2":
                if options.xiaoqu_list is not None:
                     xiaoqu_id_list = options.xiaoqu_list.split(",")
                else:
                    xiaoqu_id_list =  []
                like.crawl_house_by_xiaoqu(district,area,xiaoqu_id_list)
            elif type == "3":
                house_id = options.house
                like.crawl_house_detail("",None,None,house_id)
            else:
                like.crawl_all()
        except Exception as e:
            mail = SendMail()
            mail.send_email("房源爬虫异常提醒：{0}".format(e),msghtml=traceback.print_exc())


if __name__ == "__main__":
   h = HouseSpider()
   h.run()

